<template>
    <div>
        <el-table :data="specsList" border style="width: 100%">
            <el-table-column prop="id" label="规格编号" width="180">
            </el-table-column>
            <el-table-column prop="specsname" label="规格名称" width="180">
            </el-table-column>
            <el-table-column prop="attrs" label="规格属性">
                <template slot-scope="item">
                    <p v-for="item in item.row.attrs">
                        {{ item }}
                    </p>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>

            </el-table-column>
            <!-- 分页 -->
        </el-table>
        <el-pagination background layout="prev, pager, next" :total="count" :page-size="size" @current-change="changePage">
        </el-pagination>
    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import { specsDelete } from '../../request/api';
export default {
    data() {
        return {}
    },
    methods: {
        ...mapActions({
            getSpecsList: 'specs/getSpecsList',
            getCount: 'specs/getCount',
            change_page: 'specs/changePage'
        }),
        // 删除
        del(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                specsDelete({ id }).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        // 删除成功
                        this.$message.success(res.msg);

                        // 重新渲染视图
                        this.getSpecsList();
                        // 重新获取总条数
                        this.getCount();

                    } else {
                        // 删除失败
                        this.$message.error(res.msg);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 编辑
        edit(id) {
            this.$emit('edit', id);
        },
        // 切换分页
        changePage(num) {
            this.change_page(num)
        }
    },
    mounted() {
        this.getSpecsList();
        this.getCount();
        this.change_page(1)

    },
    watch: {},
    computed: {
        ...mapGetters({
            specsList: 'specs/specs_list',
            size: 'specs/specs_size',
            count: 'specs/specs_count'
        })
    },
    filters: {},
    components: {},
}
</script>
<style scoped>
.el-pagination {
    float: right;
}
</style>